<?php
/**
 *============================================================================
 * Cornice PHP Framework & Website Builder
 * Freeware (f) 2011 by Inforo Software & Computer
 *============================================================================
 */
 
/**
 * Class for accessing serialized objects on disk
 * @author Nicola Dinh
 * @version 2011.04.2311
 */
class data {

  /*
   * All properties
   */
  private static $secured_pso = null;

  /**
   * Default instance level constructor
   * Just decorative, should not be called due to having only static methods
   */
  public function __construct() {
    //
  }
  
  /**
   * Save object to file, ".pso" means 'PHP Serialized Object'
   * @param string $name The file name of object
   * @param object $object The object to be serialized and saved
   */
  public static function save_to_file($name,$object,$path="objects") {
    file_put_contents("data/$path/$name.pso",serialize($object));
  }
  
  /**
   * Load serialized object from file, ".pso" means 'PHP Serialized Object'
   * @param string $name The file name of object
   * @return object The object deserialized from saved data
   */
  public static function load_from_file($name,$path="objects") {
    return unserialize(file_get_contents("data/$path/$name.pso"));
  }
  
  /**
   * Check if data file exists (data file is just serialized object)
   * @param string $name The data file name
   * @return boolean 'true' if exists, 'false' otherwise
   */
  public static function file_exists($name,$path="objects") {
    return file_exists("data/$path/$name.pso");
  }
  
  /**
   * Check if a secured PSO exists in 'data/secured'
   * @param string $name The name of secured PHP serialized object
   * @return boolean 'true' if .spso file exists, 'false' otherwise
   */
  public static function spso_exists($name) {
    return file_exists("data/secured/$name.php");
  }
  
  /**
   * Save object to .spso file
   * @param string $name The file name to write on
   * @param object $object The object to write to file
   */
  public static function save_to_spso($name,$object) {
    $text  = "<?php\r\n";
    $text .= "self::\x24secured_pso = <<<SECURED_PSO\r\n";
    $text .= bin2hex(serialize($object))."\r\n";
    $text .= "SECURED_PSO;\r\n";
    $text .= "?>\r\n";
    file_put_contents("data/secured/$name.php",$text);
  }
  
  /**
   * Load object from .spso file
   * @param string $name The name of object
   */
  public static function load_from_spso($name) {
    require("data/secured/$name.php");
    return unserialize(hex2bin(self::$secured_pso));
  }
  
  /**
   * Get passwords stored in SPSO file (in 'data/secured')
   * @return array The list of passwords
   */
  public static function get_passwords() {
    if (!self::spso_exists("passwords"))
      return array();
    return self::load_from_spso("passwords");
  }
  
  /**
   * Check if a pair of 'user name' and 'password' is accessible to 
   * current module
   * @param string $user_name The user name
   * @param string $password The password for that user name
   * @return boolean 'true' if username/password accessible to current module
   */
  public static function cur_module_accepts($user_name,$password) {
    $passwords = self::get_passwords();
    $module = request::get_module_name();
    
    //scan passwords
    foreach ($passwords as $item) 
      if ($item["type"]=="module_password" && $item["module"]==$module && 
        $item["user_name"]==$user_name && $item["sha1"]==sha1($password))
        return true;
      
    //no matching found  
    return false;
  }
}